<?php
namespace app\admin\controller;

use think\Controller;

class Link extends Controller
{
    public function lst()
    {

        $linkRes = db('link')->order('id desc')->paginate(10);
        $this->assign([
            'linkRes' => $linkRes,

        ]);
        return view('list');
    }
    public function add()
    {
        if (request()->isPost()) {

            $data = input('post.');
            if ($data['link_url'] && stripos($data['link_url'], 'http://') === false) {
                $data['link_url'] = 'http://' . $data['link_url'];
            }
            //处理图片上传
            if ($_FILES['logo']['tmp_name']) {
                $data['logo'] = $this->upload();
            }
            //验证
            $validate = validate('link');
            if (!$validate->check($data)) {
                $this->error($validate->getError());

            }
            $add = db('link')->insert($data);

            if ($add) {

                $this->success('添加链接成功 ！', 'lst');
            } else {

                $this->error('添加链接失败');
            }
            return;
        }

        return view();
    }
    public function edit()
    {

        if (request()->isPost()) {
            $data = input('post.');
            if ($data['link_url'] && stripos($data['link_url'], 'http://') === false) {
                $data['link_url'] = 'http://' . $data['link_url'];
            }
            //处理图片上传
            if ($_FILES['logo']['tmp_name']) {
                $oldlinks    = db('link')->find($data['id']);
                $oldlinksImg = IMG_UPLOADS . $oldlinks['logo'];
                if (file_exists($oldlinksImg)) {
                    @unlink($oldlinksImg);
                }
                $data['logo'] = $this->upload();
            }
            //验证
            $validate = validate('link');
            if (!$validate->check($data)) {
                $this->error($validate->getError());

            }
            $save = db('link')->update($data);
            if ($save !== false) {

                $this->success('修改链接成功 ！', 'lst');
            } else {

                $this->error('修改链接失败');
            }
            return;
        }

        $id = input('id');

        $link = db('link')->find($id);

        $this->assign([
            'link' => $link,

        ]);

        return view();
    }
    public function del($id)
    {
        $del = db('link')->delete($id);

        if ($del) {

            $this->success('删除链接成功 ！', 'lst');
        } else {

            $this->error('删除链接失败');
        }
    }
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('logo');

        // 移动到框架应用根目录/public/uploads/ 目录下
        if ($file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'static' . DS . 'uploads');
            if ($info) {

                return $info->getSaveName();
            } else {
                // 上传失败获取错误信息
                echo $file->getError();
                die;
            }
        }
    }
}
